package com.common.frame.decoration

import android.graphics.Canvas
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

/**
 *
 * create by sunxy on 2020/8/20
 **/
class GridDecoration @JvmOverloads constructor(
    private val column:Int = 2, private val itemSpace:Int = 0, private val topSpace: Int = 0, private val bottomSpace: Int = itemSpace): BaseDecoration() {

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) { }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val count = parent.adapter?.itemCount ?: 0
        if (count > 0){
            val pos = parent.getChildViewHolder(view).adapterPosition
            val perAllSpace = (column-1).toFloat() / column  * itemSpace

            val left = (pos % column).toFloat() / column * itemSpace
            val right = perAllSpace - left
            val top = if (pos < column) topSpace else 0
            val bottom = if (pos < count - column) itemSpace else bottomSpace

            outRect.set(left.toInt(), top, right.toInt(), bottom)
        }
    }


}